<div class="layui-fluid layui-anim febs-anim" id="febs-order" lay-title="客户管理">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="user-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">昵称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="nickName" autocomplete="off"
                                                   placeholder="输入昵称进行检索" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">真实姓名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="trueName" autocomplete="off"
                                                   placeholder="输入客户名进行检索" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">订单号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="outTradeNo" autocomplete="off"
                                                   placeholder="输入订单号进行检索" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">订单状态</label>
                                        <div class="layui-input-inline">
                                            <select name="isSuccess">
                                                <option value="">请选择订单状态</option>
                                                <option value="1">已支付</option>
                                                <option value="0">未支付</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>搜索
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>刷新
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="user:add,user:update,user:password:reset,user:export">
                                    <i class="layui-icon">&#xe875;</i>更多
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="order_table" lay-data="{id: 'orderTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="user-status">
    {{#
    var status = {
    1: {title: '有效', color: 'green'},
    0: {title: '禁用', color: 'red'}
    }[d.status];
    }}
    <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="user-sex">
    {{#
    var sex = {
    2: {title: '保密'},
    1: {title: '女'},
    0: {title: '男'}
    }[d.sex];
    }}
    <span>{{ sex.title }}</span>
</script>
<script type="text/html" id="order-option">
    <span shiro:lacksPermission="order:view,order:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="order:view"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i><span class="febs-green">查看</span></a>
<!--    <a lay-event="del" shiro:hasPermission="order:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i><span-->
<!--            class="febs-red">删除</span></a>-->
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'upload', 'laydate', 'form', 'formSelects', 'table', 'febs'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#febs-order'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        form.render();

        initTable();

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('order_table');
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的订单');
                    } else {
                        febs.modal.confirm('删除订单', '确定删除该订单？', function () {
                            var orderIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                orderIds.push(item.orderId)
                            });
                            deleteUsers(orderIds.join(','));
                        });
                    }
                }
            },
            options: [ {
                name: 'delete',
                title: '删除订单',
                perms: 'order:delete'
            }]
        });


        table.on('tool(order_table)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;

            if (layEvent === 'detail') {
                febs.modal.view('订单信息', 'system/order/orderView/' + data.orderId, {
                    area: ['700px','800px']
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除订单', '确定删除该订单？', function () {
                    deleteUsers(data.orderId);
                });
            }
            if (layEvent === 'toSeeDetails') {
                var index = layer.open({
                    title: "查看图片详情",
                    content: data.headimgurl,
                    type: 2,
                    maxmin: true,
                    area: ['50%','50%'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    }
                });
            }
        });

        table.on('sort(order_table)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(),  initSort: sortObject,
                done: function(res, curr, count){
                    //如果是异步请求数据方式，res即为你接口返回的信息。
                    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                    //得到当前页码
                    //得到数据总量
                    var beforeCurr = curr;
                    //当前页数据的个数
                    var dataLength = res.data.length;
                    var count = res.count;
                    //如果当前页数据的个数等于0，并且总数不为0，则跳转到上一页
                    if( dataLength == 0 && count!= 0 ){
                        table.reload("order_table",{
                            page:{curr:beforeCurr - 1}
                        })
                    }
                }
            });
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'order_table',
                url: ctx + 'order/getOrderList',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'nickName', title: '昵称', minWidth: 100},
                    {field: 'trueName', title: '真实姓名', minWidth: 100},
                    {field: 'outTradeNo', title: '订单编号', minWidth: 100},
                    {field: 'body', title: '活动名称', minWidth: 100},
                    {field: 'totalFee', title: '总金额', minWidth: 100},
                    {field: 'timeStart', title: '交易时间', minWidth: 100,templet:function (d) {
                            if (d.timeStart != null){
                                return layui.util.toDateString(d.timeStart, 'yyyy年MM月dd日 HH:mm:ss')
                            }else{
                                return ''
                            }
                        }},
                    { title: '状态', templet: function (d) {
                            if(d.isSuccess==1){
                                return '<span class="layui-badge febs-tag-green">已支付</span>';
                            }else {
                                return '<span class="layui-badge febs-tag-volcano">未支付</span>'
                            }
                        }},
                    // {field: 'createTime', title: '创建时间', minWidth: 180, sort: true},
                    {title: '操作', toolbar: '#order-option', minWidth: 220}
                ]]
            });
        }

        function getQueryParams() {
            return {
                isSuccess: $searchForm.find('select[name="isSuccess"]').val(),
                nickName: $searchForm.find("input[name='nickName']").val(),
                trueName: $searchForm.find("input[name='trueName']").val(),
                outTradeNo:$searchForm.find("input[name='outTradeNo']").val(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteUsers(orderIds) {
            febs.get(ctx + 'order/delete/' + orderIds, null, function () {
                febs.alert.success('删除订单成功');
                $reset.click();
            });
        }
    })
</script>